package com.cn.wanxi.service;

import com.cn.wanxi.dto.CompanyDto;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.util.WriteRead;
import java.util.ArrayList;
import java.util.List;

import static com.cn.wanxi.util.MyPath.*;

/**
 * 公司信息的服务逻辑层
 * 1.File类   判断是否文件，是否文件夹，创建文件，文件夹，路径，三个不同构造方法
 * 2.输出流字节流OutputStream FileOutputStream  write（int）  wirte（byte【】）
 * 3.输入流字节流  输入流字符流  read    while    String，Stringbuilder，Stringbuffer
 * 4.抽象类和接口？ 什么情况下会用到抽象类和接口  实现多态
 * 5.split字符方法 String
 * 6.封装
 * 7.常量
 * 设计模式   单例   （7中）（双保险）
 */
public class CompanyService {


    /**
     * 得到公司数据
     * 数据来源：记事本
     * 1.将数据存入记事本
     * 2.将数据从记事本里面读取出来
     * 3.封装成CompanyModel
     * <p>
     * 面向对象   一个方法只能解决一件事情
     *
     * @return
     */
    public CompanyModel getCompanyModel() {
        writeNote();
        String result = WriteRead.readNote(FILE_COMPANY);
        return setData(result);
    }

    private void writeNote() {
        List<String> list = new ArrayList<>();
        CompanyDto companyDto = new CompanyDto();
        String logo = companyDto.getLogo();//100
        list.add("0logo--" + logo);
        list.add("1phone--" + companyDto.getPhone());
        list.add("2ad--" + companyDto.getAd());
        list.add("3net--" + companyDto.getNet());
        list.add("4email--" + companyDto.getEmail());
        list.add("5linephone--" + companyDto.getLinePhone());
        list.add("6address--" + companyDto.getAddress());
        list.add("7version--" + companyDto.getVersion());
        WriteRead.writeNote("FILE_COMPANY", list);
    }


    private CompanyModel setData(String result) {
//        System.out.println(result);
        String[] results = result.split(SPLIT);
        for (String re : results
        ) {
            System.out.println(re);
        }
        CompanyModel companyModel = new CompanyModel();
//        String logo = results[0];
//        String[] temp = logo.split("--");
        companyModel.setLogo(getData(results[0]));

        companyModel.setPhone(getData(results[1]));
        companyModel.setAd(getData(results[2]));
        companyModel.setNet(getData(results[3]));
        companyModel.setEmail(getData(results[4]));
        companyModel.setLinePhone(getData(results[5]));
        companyModel.setAddress(getData(results[6]));
        companyModel.setVersion(getData(results[7]));
        return companyModel;
    }

    private String getData(String result) {
        String[] temp = result.split("--");
        return temp[1];
    }
}
